use std::error::Error;

use clap::{Parser, Subcommand};
use libp2p::Multiaddr;
use linkup::nat;

#[derive(Debug, Subcommand)]
enum Commands {
    Server {
        /// 监听端口，不传则使用随机端口。
        #[clap(short = 'p', long, default_value_t = 0)]
        port: u16,
    },
    Client {
        /// 监听端口，不传则使用随机端口。
        #[clap(short = 'p', long, default_value_t = 0)]
        port: u16,

        /// 服务端地址。
        #[clap(short = 's', long)]
        server_address: Multiaddr,

        /// 探测间隔，单位为秒。
        #[clap(short = 't', long, default_value_t = 2)]
        probe_interval: u64,
    },
}

#[derive(Debug, Parser)]
#[clap(name = "linkup")]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let cli = Cli::parse();

    match cli.command {
        Commands::Client {
            server_address,
            port,
            probe_interval,
        } => nat::client::run(server_address, port, probe_interval).await,
        Commands::Server { port } => nat::server::run(port).await,
    }
}
